package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.pages.NewSurvey;
import ch.fhnw.qualiscan.core.db.pojos.CoordinatorRelevance;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.EmailHelper;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class NewSurveyTab4 extends VerticalLayout {
	
	public List<SurveyUserRole> surList;
	public RichTextArea emailEditor;
	private NewSurvey newSurvey;
	public Button saveButton;
	
	Panel p1;
	Panel p2;
	
	public NewSurveyTab4(NewSurvey newSurvey) {
		setMargin(true);
		setSpacing(true);
		this.newSurvey = newSurvey;
		buildLayout();
		
	}
	
	public void buildLayout() {
		p1 = new Panel();
		
		p1.addComponent(newSurvey.textHelper.h2(TM.get("newsurvey.tab4.p1.title")));
		p1.addComponent(newSurvey.textHelper.p(TM.get("newsurvey.tab4.p1.desc")));
		
		// save button
		saveButton = new Button(TM.get("newsurvey.tab4.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				p1.setEnabled(false);
				saveButton.setEnabled(false);
				saveToDatabase();
			}
		});
        saveButton.setIcon(Config.ICON_SAVE);
        VerticalLayout buttonWrap = new VerticalLayout();
        buttonWrap.setMargin(true, false, false, false);
        buttonWrap.addComponent(saveButton);
        
		p1.addComponent(buttonWrap);
		
		this.addComponent(p1);
	}
	
	/**
	 * Saves the data over the surveyModel to the Database
	 */
	public void saveToDatabase() {
		
		//Create Survey
		User u = new User();
		u.setId(newSurvey.userSession.getId());
		newSurvey.survey = newSurvey.surveyModel.createSurvey(newSurvey.survey, u, newSurvey.userSession.getRole());
		
		//Add Coordinator Relevances
		newSurvey.surveyModel.addQuestionWeights(newSurvey.survey, newSurvey.questionWeights);
		
		//Add Users to the Survey
		surList = newSurvey.surveyModel.addUsersToSurvey(newSurvey.participantsRoleList, newSurvey.survey);
		
		invitationTextField();
	}
	
	/**
	 * Adds a RichTextArea to the Layout containing a standard email Text
	 */
	public void invitationTextField() {
		
		p2 = new Panel();
		
		saveButton.setCaption(TM.get("newsurvey.tab4.saved"));
		
		emailEditor = new RichTextArea();
		emailEditor.setWidth("100%");
		
		String coordinator = newSurvey.userSession.getFirstname()+" "+newSurvey.userSession.getLastname(); 
		
		Label text = new Label(TM.get("email.invitationtext", coordinator, newSurvey.survey.getTitle()));
		text.setContentMode(Label.CONTENT_XHTML);
		
		emailEditor.setValue(text.getValue());
		
		p2.addComponent(newSurvey.textHelper.h2(TM.get("newsurvey.tab4.p2.title")));
		p2.addComponent(newSurvey.textHelper.p(TM.get("newsurvey.tab4.p2.desc")));
		
		p2.addComponent(emailEditor);
		
		// send email button
		Button sendInvitations = new Button(TM.get("newsurvey.tab4.p2.sendInvitation"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				sendInvitations();
			}
		});
		sendInvitations.setIcon(Config.ICON_SENDMAIL);
		p2.addComponent(sendInvitations);
		
		/* send no email button
		Button noEmail = new Button(TM.get("newsurvey.tab4.p2.noemail"));
		noEmail.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				newSurvey.urlLib.setPage("Dashboard");
			}
		});
		noEmail.setIcon(Config.ICON_SENDMAILNOT);
		*/
		
		// buttons panel
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);
		buttons.setMargin(true, false, false, false);
		buttons.addComponent(sendInvitations);
		//buttons.addComponent(noEmail);
		
		p2.addComponent(buttons);
		
		addComponent(p2);
	}
	
	public void sendInvitations() {
		p2.setEnabled(false);
		String coordinator = newSurvey.userSession.getFirstname()+" "+newSurvey.userSession.getLastname(); 
		EmailHelper eh = new EmailHelper();
		
		for(int i = 0; i < surList.size(); i++) {
			if (surList.get(i).getRole().getId() == Config.EXPERT_ID) {
				eh.sendInvitationMail(surList.get(i), TM.get("email.invitationtext.expert", coordinator, newSurvey.survey.getTitle()));
			}
			else {
				String emailText = emailEditor.getValue().toString();
				//String emailText = emailEditor.getValue().toString().replace("<br /><br /> <a href=#>http://www.dummylink.com</a> (Placeholder)", "");
				eh.sendInvitationMail(surList.get(i), emailText);
			}
			
		}
		
		newSurvey.urlLib.setPage("Dashboard");
	}
}
